//
//  LoginController.swift
//  1qwe1
//
//  Created by 赵宇桥 on 16/6/6.
//  Copyright © 2016年 赵宇桥. All rights reserved.
//

import Foundation
import UIKit

class LoginController: BaseviewController {
    
// MARK: Properties
    
    var phoneTextfield:UITextField?
    var passwordTextfield:UITextField?
    var loginBT:UIButton?
    var forgetBT:UIButton?
    var registBT:UIButton?
    var backBT:UIButton!

// MARK: life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBarHidden = true

        var y:CGFloat = 250
        let logo = UIImageView()
        logo.image = UIImage(named: "login_logo.png")
        var f = CADipRectMake(0, y, frameSize.width, frameSize.width*(324/810.0))
        logo.frame = f
        view.addSubview(logo)
        
        y += frameSize.width*(324/810.0)
        view.backgroundColor = color_weak_background
        let whiteView = UIView(frame:CADipRectMake(0, y, frameSize.width, 88 * 2))
        whiteView.backgroundColor = UIColor.whiteColor()
        view.addSubview(whiteView)
        
        var temY:CGFloat = 0.0
        var temX:CGFloat = spaceHeight
        
        var icon = UIImageView()
        icon.image = UIImage(named: "login_phone.png")
        f = CADipRectMake(temX, (88-40) / 2, 40, 40)
        icon.frame = f
        whiteView.addSubview(icon)
        
        temX += 40 + spaceHeight
        var line = UIView(frame: CADipRectMake(0,temY,frameSize.width,2))
        line.backgroundColor = color_line
        whiteView.addSubview(line)
        
        phoneTextfield = UITextField(frame: CADipRectMake(temX, temY, frameSize.width - spaceHeight * 3 - 40 , 88))
        phoneTextfield?.placeholder = "请输入手机号"
        phoneTextfield?.clearButtonMode = .WhileEditing
        phoneTextfield?.keyboardType = .NumberPad
        whiteView.addSubview(phoneTextfield!)
        
        temY += 88
        temX = spaceHeight
        
        line = UIView(frame: CADipRectMake(0,temY-1,frameSize.width,2))
        line.backgroundColor = color_line
        whiteView.addSubview(line)
        
        icon = UIImageView()
        icon.image = UIImage(named: "login_pass.png")
        f = CADipRectMake(temX, temY + (88-40) / 2, 40, 40)
        icon.frame = f
        whiteView.addSubview(icon)
        
        temX += 40 + spaceHeight
        passwordTextfield = UITextField(frame: CADipRectMake(temX, temY, frameSize.width - spaceHeight * 3  - 40 , 88))
        passwordTextfield?.placeholder = "请输入密码"
        passwordTextfield?.clearButtonMode = .WhileEditing
        passwordTextfield?.secureTextEntry = true
        whiteView.addSubview(passwordTextfield!)

        temY += 88
        line = UIView(frame: CADipRectMake(0,temY-1,frameSize.width,2))
        line.backgroundColor = color_line
        whiteView.addSubview(line)
        
        y += 88 * 2 + 16
        f = CADipRectMake(spaceHeight, y, frameSize.width - spaceHeight * 2, 88)
        loginBT = super.addButton(f, tittle: "登录", target: self, action: #selector(LoginController.buttonClick(_:)))
        view.addSubview(loginBT!)
        
        y+=88+16

        forgetBT = UIButton(type: .System)
        forgetBT!.setTitle("忘记密码？", forState: .Normal)
        var w = getStringWidthBy("忘记密码？", font: (forgetBT!.titleLabel?.font.pointSize)!)
        forgetBT!.frame = CADipRectMake(frameSize.width - w , y, w, 88)
        forgetBT!.addTarget(self, action: #selector(LoginController.buttonClick(_:)), forControlEvents: .TouchUpInside)
        view.addSubview(forgetBT!)
        
        
        
        
        registBT = UIButton(type: .System)
        let zongchang  = getStringWidthBy("还没有帐号?现在去免费注册", font: (registBT!.titleLabel?.font.pointSize)!)
        let zqianchang  = getStringWidthBy("还没有帐号?现在去", font: (registBT!.titleLabel?.font.pointSize)!)
        
        let qianbt = UILabel(labText: "还没有帐号?现在去",textSize: (registBT!.titleLabel?.font)!)
        qianbt.frame = CADipRectMake((frameSize.width-zongchang) / 2, frameSize.height - 200, zqianchang, 88)
        view.addSubview(qianbt)
        
        registBT!.setTitle("免费注册", forState: .Normal)
        w = getStringWidthBy("免费注册", font: (registBT!.titleLabel?.font.pointSize)!)
        registBT?.frame = CADipRectMake((frameSize.width-zongchang) / 2 + zqianchang, frameSize.height - 200, w, 88)
        registBT?.addTarget(self, action: #selector(LoginController.buttonClick(_:)), forControlEvents: .TouchUpInside)
        view.addSubview(registBT!)
        
        backBT = UIButton(type: .Custom)
        backBT.setBackgroundImage(UIImage(named: "but_return.png"), forState: .Normal)
        backBT.frame = CADipRectMake(10, spaceHeight, 80, 80)
        backBT.addTarget(self, action: #selector(LoginController.buttonClick(_:)), forControlEvents: .TouchUpInside)
        view.addSubview(backBT)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func buttonClick(sender: UIButton){
        
        switch sender {
        case loginBT!: break
            
        case registBT!:
            navigationController?.pushViewController(RegistController(), animated: true)
            //showViewController(RegistController(), sender: self)
        case forgetBT!: break
            
        case backBT:
            dismissViewControllerAnimated(true, completion: nil)
        
        default: break
            
        }
    }
    

}



